const {
  MemberModel
} = require('../../../utils/member-model.js')
const memberModel = new MemberModel()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    currentIntegral: 0,
    nextLevel: 1,
    nextLevelIntegral: 100,
    memberInfo: {},
    memberRights: [],
  },

  /**
   * 跳转到成长规则
   */
  goGrowRule() {
    wx.navigateTo({
      url: '/pages/memberCenter/growRule/index',
    })
  },
  /**
   * 跳转到成长记录
   */
  goGrowthRecord() {
    wx.navigateTo({
      url: '/pages/memberCenter/growthRecord/index?currentIntegral=' + this.data.currentIntegral,
    })
  },
  /**
   * 跳转到权益详情
   */
  goBenefitDetails(event) {
    var id = event.currentTarget.dataset.id || ""; //权益id
    const selectMemberRights = (this.data.memberRights || []).filter(item => {
      return item.memberRights == id
    })
    var rightsName = selectMemberRights[0].rightsName || ""; //权益名称
    var rightsImage = selectMemberRights[0].rightsImage || ""; //权益图片
    var userRule = encodeURIComponent(JSON.stringify(selectMemberRights[0].userRule)) || ""; //使用规则
    var instructions = selectMemberRights[0].instructions || ""; //权益简介
    wx.navigateTo({
      url: `/pages/memberCenter/benefitDetails/index?rightsName=${rightsName}&rightsImage=${rightsImage}&userRule=${userRule}&instructions=${instructions}`,
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    memberModel.userMemberPower({
      merchantId: wx.getStorageSync('merchantId')
    }).then(data => {
      // const memberRights = (data.memberRights || []).filter(item => {
      //   return item.memberLevel <= data.memberInfo.memberLevel
      // })
      this.setData({
        currentIntegral: data.currentIntegral,
        nextLevel: data.nextLevel,
        nextLevelIntegral: data.nextLevelIntegral,
        memberInfo: data.memberInfo,
        memberRights: data.memberRights || []
      })
    }).catch(err => {
      wx.showToast({
        title: `${err.error}`,
        icon: 'none'
      })
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  }
})